﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PitBullBusinessLogic.Payment;
using PitBullBusinessLogic;
using System.Web;
using PitBullBusinessLogic.Configuration;

namespace PitBull.Payment.Payson
{
    public class PaysonPaymentProcessor
    {
        #region Methods
        public void ProcessPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult)
        {
            processPaymentResult.PaymentStatus = PaymentStatusEnum.Pending;
        }

        /// <summary>
        /// Builds the query string and redirect user to Svea payment gateway
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public string PostProcessPayment(Order order)
        {
            Customer customer = order.Customer;
            
            string strOkUrl = HttpUtility.HtmlEncode(String.Format("{0}PaysonReturn.aspx", CommonHelper.GetStoreHost(false)));
            string strCancelUrl = HttpUtility.HtmlEncode(String.Format("{0}PaysonCancel.aspx", CommonHelper.GetStoreHost(false)));
            string strOrderSubtotal = Math.Round(order.OrderSubtotalInclTax, 2).ToString();
            string strShipping = Math.Round(order.OrderShippingInclTax, 2).ToString();
            string strPaysonGuarantee = PaysonPaymentSettings.PaysonGuarantee ? "2" : "1";

            string strMD5 = PaysonPaymentSettings.SellerEmail + ":" + strOrderSubtotal + ":" + strShipping + ":" + strOkUrl + ":" + strPaysonGuarantee;

            PaysonPaymentHelper paysonHelper = new PaysonPaymentHelper();

            RemotePost remotePostHelper = new RemotePost();
            remotePostHelper.FormName = "PaysonForm";
            remotePostHelper.Url = PaysonPaymentSettings.UseSandbox ? PaysonPaymentSettings.SandboxServerUrl : PaysonPaymentSettings.ServerUrl;

            remotePostHelper.Add("BuyerEmail", customer.Email);
            remotePostHelper.Add("AgentID", PaysonPaymentSettings.AgentID);
            remotePostHelper.Add("Description", SettingManager.StoreName + " OrderID: " + order.OrderID.ToString());
            remotePostHelper.Add("SellerEmail", PaysonPaymentSettings.SellerEmail);
            remotePostHelper.Add("Cost", strOrderSubtotal);
            remotePostHelper.Add("ExtraCost", strShipping);
            remotePostHelper.Add("OkUrl", strOkUrl);
            remotePostHelper.Add("CancelUrl", strCancelUrl);
            remotePostHelper.Add("RefNr", order.OrderID.ToString());
            remotePostHelper.Add("MD5", paysonHelper.CalculateMD5Hash(strMD5, PaysonPaymentSettings.MD5Key));
            remotePostHelper.Add("GuaranteeOffered", strPaysonGuarantee);

            // Below parameters are NOT required
            remotePostHelper.Add("BuyerFirstName", customer.FirstName);
            remotePostHelper.Add("BuyerLastName", customer.LastName);
            remotePostHelper.Add("PurchaseUrl ", SettingManager.StoreUrl);

            remotePostHelper.Post();

            return String.Empty;
        }

        public decimal GetAdditionalHandlingFee()
        {
            return 0;
        }

        public void Capture(Order order, ref ProcessPaymentResult processPaymentResult)
        {
            throw new NotImplementedException();
        }

        public void Refund(Order order, ref CancelPaymentResult cancelPaymentResult)
        {
            throw new NotImplementedException();
        }

        public void Void(Order order, ref CancelPaymentResult cancelPaymentResult)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Process recurring payment
        /// </summary>
        /// <param name="paymentInfo">Payment info required for an order processing</param>
        /// <param name="customer">Customer</param>
        /// <param name="orderGuid">Unique order identifier</param>
        /// <param name="processPaymentResult">Process payment result</param>
        public void ProcessRecurringPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult)
        {
            throw new Exception("Recurring payments not supported");
        }

        /// <summary>
        /// Cancels recurring payment
        /// </summary>
        /// <param name="order">Order</param>
        /// <param name="cancelPaymentResult">Cancel payment result</param>        
        public void CancelRecurringPayment(Order order, ref CancelPaymentResult cancelPaymentResult)
        {
        }

        #endregion

        #region Properties
        public bool CanCapture
        {
            get
            {
                return false;
            }
        }

        public bool CanRefund
        {
            get
            {
                return false;
            }
        }

        public bool CanVoid
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a recurring payment type of payment method
        /// </summary>
        /// <returns>A recurring payment type of payment method</returns>
        public RecurringPaymentTypeEnum SupportRecurringPayments
        {
            get
            {
                return RecurringPaymentTypeEnum.NotSupported;
            }
        }

        /// <summary>
        /// Gets a payment method type
        /// </summary>
        /// <returns>A payment method type</returns>
        public PaymentMethodTypeEnum PaymentMethodType
        {
            get
            {
                return PaymentMethodTypeEnum.Standard;
            }
        }

        #endregion
    }
}
